//
//  UserAccount.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/9.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import UIKit

/// 用户账号模型
/// - see:[http://open.weibo.com/wiki/OAuth2/access_token](http://open.weibo.com/wiki/OAuth2/access_token)
class UserAccount: NSObject, NSCoding {
    
    /// 过期日期
    var expireDate: NSDate?
    /// 当前授权用户的UID。
    var uid: String?
    /// 用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    /// access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expireDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    /// 用户昵称
    var screenName: String?
    /// 用户头像 180 * 180
    var avatarLarge: String?
    
    //MARK: - 构造方法 字典转模型
    ///
    /// - parameter dict: 转模型的字典
    ///
    /// - returns: 模型对象
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    
    override var description: String {
        let keys = ["access_token", "screenName", "expireDate", "uid", "avatarLarge"]
        return (dictionaryWithValuesForKeys(keys) as NSDictionary).description
    }
    
    
    //MARK: - 归档代理方法
    ///
    /// - parameter aCoder: 编码器
    func encodeWithCoder(aCoder: NSCoder) {
        // TODO: 编码分类 解码器
        
//        aCoder.encodeObject(access_token, forKey: "access_token")
//        aCoder.encodeObject(expireDate, forKey: "expireDate")
//        aCoder.encodeObject(uid, forKey: "uid")
//        aCoder.encodeObject(screenName, forKey: "screenName")
//        aCoder.encodeObject(avatarLarge, forKey: "avatarLarge")
        encodeObj(self, coder: aCoder)
    }
    
    /// 解档代理方法
    ///
    /// - parameter aDecoder: 解码器
    ///
    /// - returns: 当前对象的各个属性
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expireDate = aDecoder.decodeObjectForKey("expireDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screenName = aDecoder.decodeObjectForKey("screenName") as? String
        avatarLarge = aDecoder.decodeObjectForKey("avatarLarge") as? String
//        decodeObj(self, aDecoder: aDecoder)
    }
}


